package com.example.auv_660.auvorderload;

import com.example.auv_660.util.Constants;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;

public class AUVModelTCPHealthCheckOrderLoad extends OrderLoad {
    private boolean propellerTCPCheck;
    private boolean batterTCPCheck;
    private boolean depthsensorTCPCheck;
    private boolean lightTCPCheck;
    private HashMap<String, Integer> tcpCheck = new HashMap<String, Integer>()
    {
        {
            put("D", 0);
            put("B", 0);
            put("L", 0);
            put("P", 0);
        }
    };

    @Override
    public void doOrder(String order) {
        if (order.contains(Constants.CHECK_AUV_MODEL_TCP_HEALTH_ORDER))
        {
            try
            {
                propellerTCPCheck = InetAddress.getByName(Constants.PROPELLER_IP).isReachable(100);
                batterTCPCheck = InetAddress.getByName(Constants.BATTER_IP).isReachable(100);
                depthsensorTCPCheck = InetAddress.getByName(Constants.DEPTH_SENSOR_IP).isReachable(100);
                lightTCPCheck = InetAddress.getByName(Constants.LIGHT_IP).isReachable(100);
            }
            catch (UnknownHostException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            return;
        }
        else
        {
            OrderLoad nextOrder = this.getNextOrder();
            if (nextOrder != null)
            {
                nextOrder.doOrder(order);
            }
        }
    }

    public HashMap<String, Integer> getAUVModelTCPCheck()
    {
        tcpCheck.put("D", depthsensorTCPCheck ? 1 : 0);
        tcpCheck.put("B", batterTCPCheck ? 1 : 0);
        tcpCheck.put("L", lightTCPCheck ? 1 : 0);
        tcpCheck.put("P", propellerTCPCheck ? 1 : 0);
        return tcpCheck;
    }
}
